define(['common/js/boss-frame-angular-dev-0.1','common/js/service/boss-storage-service-0.1'],function(){
    function printDirective($timeout,asyncDataStorage,MaiDialog) {
        function link(scope, element, attrs) {
            scope.$on('watch-beginPrint',function(){
                scope.printDo();
            });
            scope.printElement=function(elem) {

                var iframe = document.createElement('IFRAME');
                var doc = null;
                document.body.appendChild(iframe);

                doc = iframe.contentWindow.document;
                doc.write('<div>' + elem.innerHTML + '</div>');
                doc.close();
                iframe.contentWindow.focus();
                iframe.contentWindow.print();
                //打印结束清除创建的iframe
                document.body.removeChild(iframe);
                $timeout(function(){
                    if(attrs.printAfter){
                        scope[attrs.printAfter]()
                    }
                },500);

            }

            scope.printDo = function(){
                if(attrs.printBefore){
                    var params=scope[attrs.printParams];
                    if(!scope[attrs.printBefore](params)){
                        console.log("print scope");
                        console.log(scope);
                        return;
                    }else{
                        console.log("print scope");
                        console.log(scope);
                    }
                }

                var elemToPrint = document.getElementById(attrs.printElementId);
                if (elemToPrint) {
                    scope.printElement(elemToPrint);
                }
            };
        }



        return {
            template : '<tmp ng-transclude ng-click="printDo()"></tmp>',
            link: link,
            transclude : true,
            replace : true,
            restrict: 'E'
        };
    }
    diretiveModule.directive('ngPrint', ['$timeout','asyncDataStorage','MaiDialog',printDirective]);
});